package edu.hnu.cloudflight.android;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.baidu.push.example.LoginActivity;

import edu.hnu.cloudflight.android.util.StringUtil;

public class FlightActivity extends FragmentActivity {

	private ViewPager viewPager;
	private List<Fragment> fragmentList;
	private List<String> titlesList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_flight);
		viewPager = (ViewPager) findViewById(R.id.flight_viewPager);
		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(new FlightCourseQueryActivity());
		fragmentList.add(new FlightNumberQueryActivity());
		fragmentList.add(new FlightReserveActivity());
		fragmentList.add(new FlightCollectActivity());
		titlesList = new ArrayList<String>();
		titlesList.add(StringUtil.getStringFromRes(R.string.flight_tabs_title1));
		titlesList.add(StringUtil.getStringFromRes(R.string.flight_tabs_title2));
		titlesList.add(StringUtil.getStringFromRes(R.string.flight_tabs_title3));
		titlesList.add(StringUtil.getStringFromRes(R.string.flight_tabs_title4));
		viewPager.setAdapter(new MainViewPager(getSupportFragmentManager()));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.flight, menu);
		setTitle(R.string.title_activity_flight);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.app_login:
			if (MyApplication.appToken == null || "".equals(MyApplication.appToken)) {
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			} else {
				new AlertDialog.Builder(FlightActivity.this).setMessage("您已经登录过，是否重新登录更换帐号？")
						.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
								startActivity(intent);
							}
						}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// do nothing
							}
						}).show();
			}

			break;
		case R.id.app_setting:
			break;

		case R.id.flight_collect:
			break;
		}
		return true;
	}

	private class MainViewPager extends FragmentStatePagerAdapter {

		public MainViewPager(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			if (fragmentList != null && fragmentList.size() > 0) {
				return fragmentList.get(arg0);
			}
			return null;
		}

		@Override
		public int getCount() {
			if (fragmentList != null && fragmentList.size() > 0) {
				return fragmentList.size();
			}
			return 0;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			if (titlesList != null && titlesList.size() > 0) {
				return titlesList.get(position);
			}
			return null;
		}

		@Override
		public float getPageWidth(int position) {
			// TODO Auto-generated method stub
			return 100 / 100.0f;
		}

	}

}
